<?php
$objXajax->registerFunction("frmreportincomebyday_load");    
function frmreportincomebyday_load($dateFrom='',$dateTo='',$reporttype=1){     

    global $lang;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $dateFrom = $dateFrom==''?SYSTEM_DATE_START  : $dateFrom;
    $dateTo = $dateTo==''?SYSTEM_DATE  : $dateTo;
    $arrsum = array();
    $addPath =''; 
    $reporttypeExcel = '';
    switch($reporttype) {
     case 1 :  $reporttypeExcel='Báo cáo doanh thu theo ngày';break;
     case 2 :  $reporttypeExcel='Báo cáo doanh thu theo người thu';break;
     case 3 :  $reporttypeExcel='Báo cáo doanh thu theo người thu theo ngày';break;
     default :  $reporttypeExcel='Báo cáo doanh thu theo ngày';break;
    }
    $addPath = '_'.$reporttype;
    $arrData = frmreportincomebyday_createArraySum($arrsum,$dateFrom,$dateTo,$reporttype);
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang."/'.$lang.'/main_reportincomebyday_grid$addPath.html");
    $oTbs->MergeBlock('blk_data', $arrData); 
    $oTbs->MergeBlock('blk_data_sum', $arrsum); 
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmreportincomebyday_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmreportincomebyday_tbl');"); 
    $objResponse->addScript('changeWaiting(1);');    

    $strCondition = 'Từ ngày'.SEP_TITLE.$dateFrom.SEP_ROW.'Đến ngày'.SEP_TITLE.$dateTo.SEP_ROW.'Lọc theo người thu'.SEP_TITLE.$reporttypeExcel  ;
    $objResponse->addScript("ObjExport.AssignTitle('frmreportincomebyday','Báo cáo doanh thu theo ngày');");                        
    $objResponse->addScript("ObjExport.AssignCondition('frmreportincomebyday','$strCondition');");                        
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}
function frmreportincomebyday_createArraySum(&$arrsum,$dateFrom,$dateTo,$reporttype){
    global $objDbUpdate;
    $hotel_id = $_SESSION['current']['hotel_id'];
    $tbl_postfix = $_SESSION['current']['tbl_postfix'];
    $groupby =' group by dateend order by dateend ';
    if($reporttype==1){
      $groupby = " group by dateend order by dateend ";
    }elseif($reporttype==2){
     $groupby = " group by user_update order by user_update ";
    }
    elseif($reporttype==3){
      $groupby = ' group by user_update,dateend order by user_update,dateend ' ; 
    }
    
    $sql= "select u.username as user_update,sum(roomprice) as roomprice,dateend,sum(totalprice) as totalprice,sum(pay) as payment from  (select user_update,price as roomprice , DATE_FORMAT(timeend, '%Y-%m-%d') as dateend,totalprice as totalprice,0 as pay
 from tbl_history_book_room$tbl_postfix  where hotel_id = '$hotel_id' and datediff(timeend,'$dateFrom')>=0 and datediff(timeend,'$dateTo')<=0 UNION ALL ";
 $sql.=" select user_created as user_update,0 as roomprice , DATE_FORMAT(date_created, '%Y-%m-%d') as dateend,0 as totalprice, money
 from tbl_pay where hotel_id = '$hotel_id' and datediff(date_created,'$dateFrom')>=0 and datediff(date_created,'$dateTo')<=0) as tbl_temp join tbl_user u on tbl_temp.user_update = u.id $groupby";
 $sumroomprice = 0;
 $sumtotalprice = 0;
 $sumpay = 0;
    $arr = $objDbUpdate->GetArray($sql);
    for($i=0;$i<count($arr);$i++){
      $arr[$i]['productprice'] = $arr[$i]['totalprice'] - $arr[$i]['roomprice'];
      $arr[$i]['totalprice']  =  $arr[$i]['totalprice']- $arr[$i]['payment'];
      $sumpay += $arr[$i]['payment'];  
      $sumroomprice += $arr[$i]['roomprice'];  
      $sumtotalprice += $arr[$i]['totalprice'];  
    }
    $arrsum[0]['payment'] = $sumpay;
    $arrsum[0]['roomprice'] = $sumroomprice;
    $arrsum[0]['totalprice'] = $sumtotalprice;
    $arrsum[0]['productprice'] = $sumtotalprice-$sumroomprice+$sumpay;

    return  $arr  ;
} 
# end region process data
?>
